package com.estronger.shopmanage.fragment.send;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.estronger.pull_to_refresh_expandablelist.CommonFooterView.State;
import com.estronger.pull_to_refresh_expandablelist.PullToRefreshBase;
import com.estronger.pull_to_refresh_expandablelist.PullToRefreshBase.Mode;
import com.estronger.pull_to_refresh_expandablelist.PullToRefreshBase.OnRefreshListener;
import com.estronger.pull_to_refresh_expandablelist.PullToRefreshListView;
import com.estronger.shopmanage.R;
import com.estronger.shopmanage.ui.send.SendActivity;
import com.estronger.shopmanage.adapter.SendListAdapter;
import com.estronger.shopmanage.base.MyApplication;
import com.estronger.shopmanage.bean.B20SendList;
import com.estronger.shopmanage.bean.B20SendList.SendListData;
import com.estronger.shopmanage.manager.NetManager;
import com.estronger.shopmanage.utils.NetUtils;
import com.estronger.shopmanage.utils.Toast;
import com.estronger.shopmanage.view.CircularProgress;
import com.google.gson.Gson;
import com.lidroid.xutils.http.RequestParams;

import java.util.ArrayList;

/**
 * 送货的五个Fregment的公共父类
 * 
 * @author Administrator
 */
public abstract class SendBaseFragment extends Fragment implements
		OnRefreshListener<ListView> {//
	// protected List<MeasureListData> datas; OnScrollListener,
	protected PullToRefreshListView mListView;
	protected boolean mHasNextPage;
	protected boolean isFirstTime = true;
	protected RequestParams params;
	protected RelativeLayout root;
	protected CircularProgress bar;
	protected TextView empty_view;
	protected SendListAdapter adapter;
	protected ArrayList<SendListData> data = new ArrayList<B20SendList.SendListData>();
	Gson gson = new Gson();
	private SendActivity activity;

	protected NetManager netManager = new NetManager() {
		@Override
		public void onNetWorkErorr(int code, String result) {
			if (null != mListView) {
				mListView.setState(State.HIDE);
				if (null != bar)
					bar.setVisibility(View.GONE);
				mListView.onRefreshComplete();
				if (data.size() == 0) {
					empty_view.setVisibility(View.VISIBLE);
				}
			}
		}

		@Override
		public void onNetWorkSuccess(int code, String result) {
			if (null != bar)
				bar.setVisibility(View.GONE);
			if (null != mListView) {
				mListView.setState(State.HIDE);
				mListView.onRefreshComplete();
				if (code == NetManager.SEND_LIST) {
					B20SendList bean = gson.fromJson(result, B20SendList.class);
					if (bean.success && bean.data != null) {

						data.clear();
						data.addAll(bean.data);
						adapter.notifyDataSetChanged();
						if (data.size() > 0
								&& empty_view.getVisibility() == View.VISIBLE) {
							empty_view.setVisibility(View.GONE);
						}
						if (data.size() == 0) {
							empty_view.setVisibility(View.VISIBLE);
						}
						// activity.setIndicatorTitle(gettype(), data.size() +
						// "");
					} else {
						empty_view.setVisibility(View.VISIBLE);
					}
				}
			}
		}
	};

	public void connect(final String gettype) {
		if(activity==null){
			activity = (SendActivity) getActivity();
		}else {
			activity.getTitleNum();
		}
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				params = new RequestParams();
				params.addBodyParameter("usercode", MyApplication.getUserCode());
				params.addBodyParameter("gettype", gettype);
				params.addBodyParameter("sf_action", "dlvgettasklist");
				netManager.connectWork(params, NetManager.SEND_LIST);
			}
		}, 1000);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// return super.onCreateView(inflater, container, savedInstanceState);
		activity = (SendActivity) getActivity();
		root = (RelativeLayout) View.inflate(activity,
				R.layout.fragment_history, null);
		mListView = (PullToRefreshListView) root
				.findViewById(R.id.success_listview);

		bar = (CircularProgress) root.findViewById(R.id.loding_view);
		empty_view = (TextView) root.findViewById(R.id.empty_view);
		// mListView.setOnScrollListener(this);
		mListView.setOnRefreshListener(this);
		mListView.setMode(Mode.PULL_FROM_START);
		mListView.setState(State.HIDE);
		boolean isAll;
		if ("4".equals(gettype())) {
			isAll = true;
		} else {
			isAll = false;
		}
		adapter = new SendListAdapter(activity, data, gson, isAll);
		mListView.setAdapter(adapter);
		return root;
	}

	protected abstract boolean needConnect();

	protected abstract String gettype();

	protected void loadData() {
		if (!NetUtils.isNetworkConnected(getActivity())) {
			mListView.postDelayed(new Runnable() {
				@Override
				public void run() {
					mListView.onRefreshComplete();
					Toast.show(activity, "当前无网络链接！", 2000);
				}
			}, 1000);
		} else {
			MyApplication.getHandler().postDelayed(new Runnable() {
				@Override
				public void run() {
					// mListView.setState(State.LOADING);
					// isLoadMore = true;
					connect(gettype());
				}
			}, 1000);
		}
	}

	@Override
	public void onRefresh(PullToRefreshBase<ListView> refreshView) {
		loadData();
	}

}
